/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gerenciadoc.dao;

import gerenciadoc.utils.HibernateUtil;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 *
 * @author Rafael
 */
public abstract class GenericaDAO <K>{

    private final Session s;

    public GenericaDAO()
    {
        s = HibernateUtil.getSessionFactory().openSession();
    }

    public void inserir(K obj){
        try {

        s.getTransaction().begin();
        s.saveOrUpdate(obj);
        s.getTransaction().commit();
        s.close();
        }
        catch (HibernateException erro){
            JOptionPane.showMessageDialog(null, "Erro na inserção "+erro);
            s.getTransaction().rollback();
            s.close();
        }

    }

    public void apagar(K obj) {

        try {

        s.getTransaction().begin();
        s.delete(obj);
        s.getTransaction().commit();
        }
        catch (HibernateException erro){
            JOptionPane.showMessageDialog(null, "Erro na exclusão "+erro);
            s.getTransaction().rollback();
            s.close();
        }

    }


}
